package offer_test_60;

//Question 1输一个递增排序的数组和一个数字s,在数组中查找两个数使得它们的和正好是s。
//如果有多对数字的和等于s，输出任意一对即可。例如：输入数组{1,2,4,7,11,15}和数字为15.输出4和11.
//Question 2输入一个正数s，打印出所有和为s的连续正数序列（至少含两个数）。
//例如输入15，由于1+2+3+4+5=4+5+6=7+8=15，所以结果打印出3个连续序列1-5、4-6、和7-8.
public class one_TwoNumbersWithSum {
	public static void main(String[] args) {
		int[] array = { 1, 2, 4, 7, 11, 15 };
		// getTwoNumbers(array, 15);
		getSequence(15);
	}

	public static void getTwoNumbers(int[] array, int aim) {
		int head = 0;
		int tail = array.length - 1;
		while (head < tail) {
			while ((array[head] + array[tail] < aim) && head < tail) {
				head++;
			}
			if (array[head] + array[tail] == aim) {
				System.out.println(array[head] + " " + array[tail]);
				return;
			}
			while ((array[head] + array[tail] > aim) && head < tail) {
				tail--;
			}
			if (array[head] + array[tail] == aim) {
				System.out.println(array[head] + " " + array[tail]);
				return;
			}
		}
		System.out.println("no");
	}

	public static void getSequence(int num) {
		int begin = 1;
		int end = num / 2 + 1;
		int now = begin - 1;
		int count = 0;
		while (now <= end) {
			while (count < num) {
				now++;
				count += now;
			}
			if (count == num) {
				System.out.println("from" + begin + "to" + now);
				count -= begin;
				begin++;
			}
			while (count > num) {
				count -= begin;
				begin++;
			}
		}
	}
}
